<?php
/**
 * @filesource library/Clu/Controller/Plugin/Hvn.php
 * @author Tran Van Hoang
 * @abstract :  now setting module include path is included in Acl plugin
 * 				the layout and such is handled in the Clu_Controller_Action_Hvn 
 * 				which registers Block/IndexController/IndexAction for the last stack action 
 * 
 */

class Clu_Controller_Plugin_Hvn extends Zend_Controller_Plugin_Abstract
{

	/** 
	 * 29.05.09
	 * Has been moved from Clu_Controller_Plugin_Acl to here
	 * This will check ACL for permission on modules.
	 * 
	 *  - module-leve checking is done here. Thus it only solves some problem
	 *  - action-level checking is done inside the controller
	 * 
	 * TODO: What about controller-level? At least there are 'index' and 'ajax','block' controllers for almost
	 *   every module
	 */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName();
        //$action = $request->getActionName(); $controller = $request->getControllerName();

        //TODO: add all the modules at once?
    	set_include_path(  
            get_include_path() . PATH_SEPARATOR .  
            APP_PATH.'/modules/'.$module 
        );

        /*
        $sess = new Zend_Session_Namespace(CLU_NS);
    	if (!strpos($sess->userPermissions, 'administer everything') && 
    		($module == 'category' || $module == 'block' || $module == 'user'))
    	{
    		die('You are not allowed to do this');
    	}
		*/
        
//		die($module);
        /*    
        $requestActionUrl = '/'.$module.'/'.$controller.'/'.$action;
        
		$sess = new Zend_Session_Namespace(CLU_NS);
		$userPermissions = $sess->userPermissions;
		return;
		
		
		$adminModules = array('block', 'category'); //user->role, user->list
		//TODO: articles? comments
		
		$allRequestPermissions = Zend_Registry::get('permConfiguration')->moduleRequestPermissions;
		$neededPermission = sprintf($allRequestPermissions->{$requestActionUrl}, $module);
		//die($this->view->hvn(array('action'=>'new','module'=>'place')));
		
		if (!$neededPermission ) //OK
		{
    		Zend_Registry::get('logger')->log('Plugin.Acl.preDispatch: No perm defined for request', Zend_Log::INFO);
		}
		else  
		{	
			if(!in_array($neededPermission, explode(',', $userPermissions)) &&
				!in_array('administer '.$module, explode(',', $userPermissions)) //not admin either
				)
				 $this->_response->setRedirect('/en/user/error/auth');
		}
		
		//TODO: Some logging here
        //if (!$this->_isAllowed($request))
        //    $this->_response->setRedirect('/en/user/error/auth');
		//TODO: when deletig something. Only 'admin' is able to delete permanently, else only set status
		*/
    }
    
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

//    public function preDispatch(Zend_Controller_Request_Abstract $request)
//    {
//    	Zend_Registry::get('logger')->log('Plugin.Hvn.preDispatch', Zend_Log::INFO);
//    	//add module path
//        set_include_path(  
//            get_include_path() . PATH_SEPARATOR .  
//            APP_PATH.'/modules/'.$request->getModuleName() 
//            );
//    }

//    public function dispatchLoopShutdown(Zend_Controller_Request_Abstract $request)
//    {
//        $layout = Zend_Layout::getMvcInstance();
//        $layout->currentPage = $request->getModuleName();
//    }
        
//    protected function _moduleChange($moduleName)
//    {
//        $this->getLayout()->setLayoutPath(
//            dirname(dirname(
//                $this->getLayout()->getLayoutPath()
//            ))
//            . DIRECTORY_SEPARATOR . $moduleName . '/views/layouts'
//        );
//        $this->getLayout()->setLayout($moduleName);
//    }
            
//    public function postDispatch(Zend_Controller_Request_Abstract $request)
//    {
//    	Zend_Registry::get('logger')->log('Plugin.Hvn.postDispatch', Zend_Log::INFO);
//    
//    }


?>